package com.example.service;

import com.example.entity.Account;
import com.example.entity.Financial;
import com.example.entity.User;
import com.example.mapper.FinancialMapper;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FinancialService {

    @Resource
    FinancialMapper financialMapper;
    @Resource
    private UserMapper  userMapper;

    public void add(Financial financial) {
        financialMapper.insert(financial);
    }

    public void update(Financial financial) {
        financialMapper.updateById(financial);
    }

    public void deleteById(Integer id) {
        financialMapper.deleteById(id);
    }

    public List<Financial> selectAll(Financial financial) {
        return financialMapper.selectAll(financial);
    }

    public PageInfo<Financial> selectPage(Integer pageNum, Integer pageSize, Financial financial) {
        Account currentUser = TokenUtils.getCurrentUser();
        if ("USER".equals(currentUser.getRole())) {
            String currentUserIdStr = currentUser.getId().toString();
            User user = userMapper.selectById(currentUserIdStr);
            if ("主管".equals(user.getLevel())) {
                financial.setDepartmentId(user.getDepartmentId());
            }
        }
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Financial> list = financialMapper.selectAll(financial);
        return PageInfo.of(list);
    }

}